package com.victor;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication
@EnableTransactionManagement
public class SYYJApplication extends SpringBootServletInitializer {

	/**
	 * springboot主启动类
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(SYYJApplication.class, args);
	}

	/**
	 * 嵌入式Servlet容器:
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SYYJApplication.class);
	}

	/**
	 * 跨域过滤器
	 * 
	 * @return
	 */
	private CorsConfiguration buildConfig() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*");
		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("*");
		return corsConfiguration;
	}

	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig());
		return new CorsFilter(source);
	}

	/**
	 * 文件解析器缓存
	 * 
	 * @return
	 */
	@Bean
	MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setLocation("c://temp");
		return factory.createMultipartConfig();
	}

	/**
	 * 事务管理器
	 * 
	 * @param dataSource
	 * @return
	 */
//	@Bean
//	public PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
//		return new DataSourceTransactionManager(dataSource);
//
//	}

}
